package com.mall.b2c.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.b2c.domain.member.ConsultDomain;
import com.mall.b2c.enums.ConsultType;
import com.mall.b2c.service.ConsultService;
import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.b2c.util.ShopUtil;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.controller.BaseController;
import com.mall.common.exception.NoticeException;
import com.mall.common.util.EnumUtils;
import com.mall.common.util.ResponseUtils;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.Validations;
import com.mall.common.web.Servlets;

/**
 * 咨询、建议、投诉、纠错
 * @author lzx
 *
 */
@Controller
@RequestMapping("/admin/b2c/consult")
public class ConsultController extends BaseController {
	
	@Resource private ConsultService consultService;
	
	/**
	 * 跳转到列表页面
	 */
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String list(Model model){
		model.addAttribute("types", EnumUtils.enumProp2I18nList(ConsultType.class));
		return "/admin/b2c/consult/list";
	}

	/**
	 * 获取列表信息
	 */
	@RequestMapping(value="/queryData", method=RequestMethod.POST)
	@ResponseBody
	public Pager queryData(Pager pager, HttpServletRequest request){
    	Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
		List<SearchFilter> filters = SearchFilter.parse(filterParams);
    	pager = consultService.findPage(pager, filters);
		return JsonDataGridHelper.createJSONData(pager);
    }
	
	/**
	 * 编辑
	 */
	@Validations(
		requiredFields = { 
			@RequiredFieldValidator(fieldName = "id", message = "ID非法!")
		}
	)
	@RequestMapping(value="/view", method=RequestMethod.GET)
	public String edit(Long id, Model model){
		ConsultDomain consult = consultService.find(id);
		model.addAttribute("consult", consult);
		return "/admin/b2c/consult/view";
	}
	
	/**
	 * 修改
	 */
	@Validations(
		requiredFields = {
			@RequiredFieldValidator(fieldName = "id", message = "ID非法!")	
		}
	)
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public String update(ConsultDomain consult, HttpServletResponse response){
		ConsultDomain dbConsult = consultService.find(consult.getId());
		if(dbConsult == null){
			throw new NoticeException("该信息不存在");
		}
		
		if(StringUtils.isNotBlank(consult.getReturnContent())){
			dbConsult.setReturnContent(consult.getReturnContent());
			dbConsult.setReturnStatus(true);
			dbConsult.setReturner(ShopUtil.getUser().getUsername());
		}
		
		consultService.update(dbConsult);
		return ResponseUtils.renderJSON(successMessage, response);
	}
	
    /**
	 * 删除
	 */
	@Validations(
		requiredFields = {
			@RequiredFieldValidator(fieldName = "ids", message = "ids非法")	
		}
	)
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	@ResponseBody
	public String delete(Long[] ids, HttpServletResponse response){
		consultService.remove(ids);
    	return ResponseUtils.renderJSON(successMessage, response);
    }
	
}
